﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

using EntLibEC.Cmn;
using EntLibEC.Biz.Basic;
using EntLibEC.Info.Basic;
using EntLibEC.Biz;
using System.Text;

namespace EntLibEC.View.Basic
{
    public partial class CategoryCustomizedV2 : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                InitPage();
        }

        protected void InitPage()
        {
            catOrignal.BindCategory();
            destCategory.BindCategory();
            catCustomized.BindCategory();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable("CategoryList");
            table.Columns.Add(new DataColumn("SysNo", typeof(int)));
            table.Columns.Add(new DataColumn("CategoryID", typeof(string)));
            table.Columns.Add(new DataColumn("CategoryName", typeof(string)));
            table.Columns.Add(new DataColumn("StatusName", typeof(string)));

            if (catOrignal.C1SysNo == AppConst.IntNull && catOrignal.C2SysNo == AppConst.IntNull)
            {
                lblCategoryType.Text = "产品大类目录";
                ViewState["orgCategoryType"] = 1;

                SortedList htC1 = CategoryManager.GetInstance().GetC1List();
                foreach(Category1Info item in htC1.Keys)
                {
                    DataRow row = table.NewRow();
                    row["SysNo"] = item.SysNo;
                    row["CategoryID"] = item.C1ID;
                    row["CategoryName"] = item.C1Name;
                    row["StatusName"] = AppEnum.GetBiStatus(item.Status);
                    table.Rows.Add(row);
                }

            }
            else if (catOrignal.C1SysNo != AppConst.IntNull && catOrignal.C2SysNo == AppConst.IntNull)
            {
                lblCategoryType.Text = "产品中类目录";
                ViewState["orgCategoryType"] = 2;

                SortedList htC2 = CategoryManager.GetInstance().GetC2List();
                foreach (Category2Info item in htC2.Keys)
                {
                    if (item.C1SysNo == catOrignal.C1SysNo)
                    {
                        DataRow row = table.NewRow();
                        row["SysNo"] = item.SysNo;
                        row["CategoryID"] = item.C2ID;
                        row["CategoryName"] = item.C2Name;
                        row["StatusName"] = AppEnum.GetBiStatus(item.Status);
                        table.Rows.Add(row);
                    }
                }

            }
            else if (catOrignal.C2SysNo != AppConst.IntNull)
            {
                lblCategoryType.Text = "产品小类目录";
                ViewState["orgCategoryType"] = 3;

                SortedList htC3 = CategoryManager.GetInstance().GetC3List();
                foreach (Category3Info item in htC3.Keys)
                {
                    if (item.C2SysNo == catOrignal.C2SysNo)
                    {
                        DataRow row = table.NewRow();
                        row["SysNo"] = item.SysNo;
                        row["CategoryID"] = item.C3ID;
                        row["CategoryName"] = item.C3Name;
                        row["StatusName"] = AppEnum.GetBiStatus(item.Status);
                        table.Rows.Add(row);
                    }
                }
            }

            GridView1.DataSource = table;
            GridView1.DataBind();

        }

        protected void btnQuery2_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable("CategoryList");
            table.Columns.Add(new DataColumn("SysNo", typeof(int)));
            table.Columns.Add(new DataColumn("OriginCategorySysNo", typeof(string)));
            table.Columns.Add(new DataColumn("CategoryName", typeof(string)));
            table.Columns.Add(new DataColumn("StatusName", typeof(string)));
            table.Columns.Add(new DataColumn("URL", typeof(string)));
            table.Columns.Add(new DataColumn("Operation", typeof(string)));

            if (catCustomized.C1SysNo == AppConst.IntNull && catCustomized.C2SysNo == AppConst.IntNull)
            {
                lblCategoryType2.Text = "产品大类目录";

                List<CustomizedCategoryInfo> customizedCategoryList = CustomizedCategoryManager.GetInstance().GetCategoryList(1);
                foreach (CustomizedCategoryInfo item in customizedCategoryList)
                {
                    DataRow row = table.NewRow();
                    row["SysNo"] = item.SysNo;
                    row["OriginCategorySysNo"] = item.OriginCategorySysNo;
                    row["CategoryName"] = item.CategoryName;
                    row["StatusName"] = AppEnum.GetBiStatus(item.Status);
                    row["URL"] = item.URL;

                    string opertion = "<a href=\"javascript:openWindowS3('CategoryCustomizedOpt.aspx?opt=update&sysno=" + item.SysNo.ToString() + "')" + "\">更新</a>";
                    opertion += "&nbsp;&nbsp;&nbsp;";
                    opertion += "<a href=\"javascript:openWindowS3('CategoryCustomizedOpt.aspx?opt=delete&sysno=" + item.SysNo.ToString() + "')" + "\">删除</a>";
                    row["Operation"] = opertion;

                    table.Rows.Add(row);
                }

            }
            else if (catCustomized.C1SysNo != AppConst.IntNull && catCustomized.C2SysNo == AppConst.IntNull)
            {
                lblCategoryType2.Text = "产品中类目录";

                List<CustomizedCategoryInfo> customizedCategoryList = CustomizedCategoryManager.GetInstance().GetCategoryList(2);
                foreach (CustomizedCategoryInfo item in customizedCategoryList)
                {
                    if (item.ParentSysNo == catCustomized.C1SysNo)
                    {
                        DataRow row = table.NewRow();
                        row["SysNo"] = item.SysNo;
                        row["OriginCategorySysNo"] = item.OriginCategorySysNo;
                        row["CategoryName"] = item.CategoryName;
                        row["StatusName"] = AppEnum.GetBiStatus(item.Status);
                        row["URL"] = item.URL;

                        string opertion = "<a href=\"javascript:openWindowS3('CategoryCustomizedOpt.aspx?opt=update&sysno=" + item.SysNo.ToString() + "')" + "\">更新</a>";
                        opertion += "&nbsp;&nbsp;&nbsp;";
                        opertion += "<a href=\"javascript:openWindowS3('CategoryCustomizedOpt.aspx?opt=delete&sysno=" + item.SysNo.ToString() + "')" + "\">删除</a>";
                        row["Operation"] = opertion;

                        table.Rows.Add(row);
                    }
                }

            }
            else if (catCustomized.C2SysNo != AppConst.IntNull)
            {
                lblCategoryType2.Text = "产品小类目录";

                List<CustomizedCategoryInfo> customizedCategoryList = CustomizedCategoryManager.GetInstance().GetCategoryList(3);
                foreach (CustomizedCategoryInfo item in customizedCategoryList)
                {
                    if (item.ParentSysNo == catCustomized.C2SysNo)
                    {
                        DataRow row = table.NewRow();
                        row["SysNo"] = item.SysNo;
                        row["OriginCategorySysNo"] = item.OriginCategorySysNo;
                        row["CategoryName"] = item.CategoryName;
                        row["StatusName"] = AppEnum.GetBiStatus(item.Status);
                        row["URL"] = item.URL;

                        string opertion = "<a href=\"javascript:openWindowS3('CategoryCustomizedOpt.aspx?opt=update&sysno=" + item.SysNo.ToString() + "')" + "\">更新</a>";
                        opertion += "&nbsp;&nbsp;&nbsp;";
                        opertion += "<a href=\"javascript:openWindowS3('CategoryCustomizedOpt.aspx?opt=delete&sysno=" + item.SysNo.ToString() + "')" + "\">删除</a>";
                        row["Operation"] = opertion;

                        table.Rows.Add(row);
                    }
                }
            }

            GridView2.DataSource = table;
            GridView2.DataBind();
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            ImportIntoCustomizedCategory(true);
        }

        private void ImportIntoCustomizedCategory(bool isOnlySelected)
        {
            if (!base.CheckAccess((int)AppEnum.Privilege.SetCategoryCustomized, false, lblMessage))
                return;

            int orgCategoryType = Convert.ToInt32(ViewState["orgCategoryType"]);
            int parentSysNo = 0;

            if (orgCategoryType == 1 && destCategory.C1SysNo == AppConst.IntNull && destCategory.C2SysNo == AppConst.IntNull)
            {
                parentSysNo = 0;
            }
            else if (orgCategoryType == 2 && destCategory.C1SysNo != AppConst.IntNull && destCategory.C2SysNo == AppConst.IntNull)
            {
                parentSysNo = destCategory.C1SysNo;
            }
            else if (orgCategoryType == 3 && destCategory.C2SysNo != AppConst.IntNull)
            {
                parentSysNo = destCategory.C2SysNo;
            }
            else
            {
                Assert(lblMessage, "选择的源目录结构和定制的目录结构不一致！必须要保持一致！", -1);
                return;
            }

            string strSelect = string.Empty;
            StringBuilder sb = new StringBuilder();
            CustomizedCategoryManager ccm = CustomizedCategoryManager.GetInstance();

            foreach (GridViewRow GR in this.GridView1.Rows)
            {
                if (isOnlySelected)
                {
                    CheckBox chkSelect = (CheckBox)GR.FindControl("chkSelect");
                    if (chkSelect.Checked)
                    {
                        int categorySysNo = Convert.ToInt32(this.GridView1.DataKeys[GR.RowIndex].Value);
                        if (ccm.CheckExist(categorySysNo, orgCategoryType, parentSysNo))
                        {
                            sb.AppendLine("目录 - " + categorySysNo.ToString() + "已经存在，不能重复导入！<br />");
                        }
                        else
                        {
                            ccm.Insert(categorySysNo, orgCategoryType, parentSysNo, base.sInfo.User.SysNo);
                            sb.AppendLine("成功导入目录 - " + categorySysNo.ToString() + "<br />");
                        }
                    }
                }
                else
                {
                    int categorySysNo = Convert.ToInt32(this.GridView1.DataKeys[GR.RowIndex].Value);
                    if (ccm.CheckExist(categorySysNo, orgCategoryType, parentSysNo))
                    {
                        sb.AppendLine("目录 - " + categorySysNo.ToString() + "已经存在，不能重复导入！<br />");
                    }
                    else
                    {
                        ccm.Insert(categorySysNo, orgCategoryType, parentSysNo, base.sInfo.User.SysNo);
                        sb.AppendLine("成功导入目录 - " + categorySysNo.ToString());
                    }
                }
            }

            Assert(lblMessage, strSelect + sb.ToString(), 1);
        }

        protected void btnImportAll_Click(object sender, EventArgs e)
        {
            ImportIntoCustomizedCategory(false);
        }

        protected void btnInitializeCustomizedCategory_Click(object sender, EventArgs e)
        {
            if (!base.CheckAccess((int)AppEnum.Privilege.SetCategoryCustomized, false, lblMessage))
                return;

            CustomizedCategoryManager ccm = CustomizedCategoryManager.GetInstance();

            try
            {
                ccm.InitializeCustomizedCategory(base.sInfo.User.SysNo);

                Assert(lblMessage, "成功初始化前台产品目录结构！", 1);
            }
            catch (Exception ex)
            {
                Assert(lblMessage, ex.Message, -1);
            }
        }
    }
}
